#include "TaxonomyHandler.h"
#include<xercesc\sax2\Attributes.hpp>
TaxonomyHandler::TaxonomyHandler(void):m_pCurrentConception(NULL)
{
}


TaxonomyHandler::~TaxonomyHandler(void)
{
	for(map<string,ServiceConception*>::iterator it = m_mConceptions.begin();
		it != m_mConceptions.end();it++)
		delete it->second;
}

void TaxonomyHandler::startDocument(){
#ifdef __DEBUG__
	printf("starting........\n");
#endif
}

void TaxonomyHandler::endDocument(){
#ifdef __DEBUG__
	printf("...........ends.\n");
#endif
}


void TaxonomyHandler::startElement(const XMLCh* const uri, 
		const XMLCh* const localname, 
		const XMLCh* const qname, 
		const Attributes& attrs){
			XercesString kstrRDF = L"RDF";
			XercesString kstrClass = L"Class";
			XercesString kstrSubClass = L"subClassOf";
			XercesString kstrThing = L"Thing";
			XercesString kstrType = L"type";
			XercesString attriID = L"rdf:ID";
			XercesString attriResource = L"rdf:resource";



			if(!XMLString::compareString(kstrClass,localname)){
				XercesString wstrID = attrs.getValue(attriID) ;
				char* id = CharUtil::w2c(wstrID);
				
				ServiceConception* pCurrent =new  ServiceConception(id);
				m_pCurrentConception = pCurrent;
				m_mConceptions.insert(make_pair(string(pCurrent->GetID()),pCurrent));

			}
			else if(!XMLString::compareString(kstrSubClass,localname)){
				XercesString wstrSuperior = attrs.getValue(attriResource);
				char* superior = CharUtil::w2c(wstrSuperior);
				string str_superior(superior);
				delete superior;

				str_superior = str_superior.substr(1,str_superior.size()-1);
				map<string,ServiceConception*>::iterator it = m_mConceptions.find(str_superior);
				if(it != m_mConceptions.end())
					m_pCurrentConception->SetSuperior(it->second);
			}
			else if (!XMLString::compareString(kstrThing,localname)){
				XercesString wstrID = attrs.getValue(attriID) ;
				char* id = CharUtil::w2c(wstrID);
				string str_id(id);
				delete id;

				this->m_sLast = str_id.c_str();
			}
			else if (!XMLString::compareString(kstrType,localname)){
				XercesString wstrCon = attrs.getValue(attriResource);
				char* con = CharUtil::w2c(wstrCon);
				string str_con(con);
				delete con;
				str_con = str_con.substr(1,str_con.size()-1);

				m_mConInst.insert(make_pair(m_sLast,str_con));
			}

}

void TaxonomyHandler::endElement(const XMLCh* const uri, 
		const XMLCh* const localname,
		const XMLCh* const qname){
			
}


vector<string> TaxonomyHandler::GetConceptionsFromInstance(const string instance){
	vector<string> results;
	map<string,string>::iterator it = m_mConInst.find(instance);
	if(it == m_mConInst.end())
		return results;
	string concept = it->second;
	concept = concept.substr(1,concept.size()-1);

	return GetConceptions(concept);
}


vector<string> TaxonomyHandler::GetConceptions(const string concept){
	vector<string> results;

	map<string,ServiceConception*>::iterator it = m_mConceptions.find(concept);
	if(it == m_mConceptions.end())
		return results;
	ServiceConception* sc = it->second;

	while(NULL != sc){
		string tmp = string(sc->GetID());
		tmp = tmp.substr(3,tmp.size()-3);
		results.push_back(tmp);
		sc = sc->GetSuperior();
	}
	return results;
}

void TaxonomyHandler::saveInstConRelation(FILE* fp){
	for(map<string,string>::iterator it = m_mConInst.begin();
		it!=m_mConInst.end();it++)
		fprintf(fp,"%s :%s\n",it->first.c_str(),it->second.c_str());
}

void TaxonomyHandler::saveConSubRelation(FILE* fp){
	for(map<string,ServiceConception*>::iterator it = m_mConceptions.begin();
		it != m_mConceptions.end();it++)
		fprintf(fp,"%s :%s\n",it->first.c_str(),it->second->GetSuperior() == NULL?"NULL":it->second->GetSuperior()->GetID());
}

void TaxonomyHandler::SaveAsMyFormat(const char* inst_con,const char* con_sub){
	FILE* fp;
	if((fp=fopen(inst_con,"w"))==NULL){
		printf("Error:file cannot open!\n");
		getchar();
		exit(-1);
	}
	saveInstConRelation(fp);
	fclose(fp);

	if((fp=fopen(con_sub,"w"))==NULL){
		printf("Error:file cannot open!\n");
		getchar();
		exit(-1);
	}
	saveConSubRelation(fp);
	fclose(fp);
}